package com.cuit.zuke.hotel.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cuit.zuke.hotel.service.HotelSetService;
import com.cuit.zuke.common.helper.MD5;
import com.cuit.zuke.hotel.model.hotel.HotelSet;
import com.cuit.zuke.common.util.Result;
import com.cuit.zuke.hotel.vo.hotel.HotelSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "酒店设置管理接口")
@RestController
@RequestMapping("/hotel/hotelSet")
//@CrossOrigin
public class HotelSetController {

    @Autowired
    private HotelSetService hotelSetService;

    /**
     * 获取所有酒店设置
     * @return 所有酒店设置数据
     */
    @ApiOperation(value = "获取所有酒店设置")
    @GetMapping("findAll")
    public Result findAllHotelSet() {
        List<HotelSet> list = hotelSetService.list();
        return Result.ok().setData(list);
    }

    /**
     * 逻辑删除酒店设置
     * @param id 酒店设置id
     * @return 1 or 0
     */
    @ApiOperation(value = "逻辑删除酒店设置")
    @DeleteMapping("{id}")
    public Result removeHotelSet(@PathVariable Long id) {
        boolean flag = hotelSetService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error("id值错误或已删除");
        }
    }

    /**
     * 条件查询带分页酒店设置
     * @param current 当前页
     * @param limit 每页记录数
     * @param hotelSetQueryVo 查询条件
     * @return 符合条件的酒店设置数据
     */
    @ApiOperation(value = "条件查询带分页酒店设置")
    @PostMapping("findPageHotelSet/{current}/{limit}")
    public Result findPageHotelSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody
                                          (required = false) HotelSetQueryVo hotelSetQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<HotelSet> page = new Page<>(current, limit);

        //构建条件
        QueryWrapper<HotelSet> wrapper = new QueryWrapper<HotelSet>();

        String zkname = hotelSetQueryVo.getZkname();// 酒店名称
        String zkcode = hotelSetQueryVo.getZkcode();// 酒店编号

        if (!StringUtils.isEmpty(zkname)) {
            wrapper.like("zkname", hotelSetQueryVo.getZkname());
        }
        if (!StringUtils.isEmpty(zkcode)) {
            wrapper.eq("zkcode", hotelSetQueryVo.getZkcode());
        }

        // 调用方法实现分页查询
        Page<HotelSet> pageHotelSet = hotelSetService.page(page, wrapper);

        // 返回结果
        return Result.ok().setData(pageHotelSet);
    }

    /**
     * 添加酒店设置
     * @param hotelSet hotelSet实体
     * @return 1 or 0
     */
    @ApiOperation(value = "添加酒店设置")
    @PostMapping("saveHotelSet")
    public Result saveHotelSet(@RequestBody HotelSet hotelSet) {
        // 设置状态 1 使用 , 0 不能使用
        hotelSet.setStatus(1);
        // 签名秘钥
        Random random = new Random();
        hotelSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        //调用service
        boolean save = hotelSetService.save(hotelSet);
        if (save) {
            return Result.ok();
        } else {
            return Result.error("添加错误");
        }
    }

    /**
     * 根据id查询酒店设置
     * @param id 酒店设置id
     * @return 酒店设置数据
     */
    @ApiOperation(value = "根据id查询酒店设置")
    @GetMapping("getHotelSet/{id}")
    public Result getHotelSet(@PathVariable Long id) {
        HotelSet hotelSet = hotelSetService.getById(id);
        return Result.ok().setData(hotelSet);
    }

    /**
     * 修改酒店设置
     * @param hotelSet hotelSet实体
     * @return 1 or 0
     */
    @ApiOperation(value = "修改酒店设置")
    @PostMapping("updateHotelSet")
    public Result updateHotelSet(@RequestBody HotelSet hotelSet) {
        boolean flag = hotelSetService.updateById(hotelSet);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error("修改失败");
        }
    }

    /**
     * 批量删除酒店设置
     * @param idList id集合
     * @return 1 or 0
     */
    @ApiOperation(value = "批量删除酒店设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHotelSet(@RequestBody List<Long> idList) {
        hotelSetService.removeByIds(idList);
        return Result.ok();
    }

    /**
     * 锁定和解锁酒店设置
     * @param id 酒店设置id
     * @param status 状态，1锁定，0未锁定
     * @return 1 or 0
     */
    @ApiOperation(value = "锁定和解锁酒店设置")
    @PutMapping("lockHotelSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        //根据 id 查询酒店设置信息
        HotelSet hotelSet = hotelSetService.getById(id);
        //设置状态
        hotelSet.setStatus(status);
        //调用方法
        hotelSetService.updateById(hotelSet);
        return Result.ok();
    }

    /**
     * 发送签名密钥
     * @param id 酒店设置id
     * @return 1 or 0
     */
    @ApiOperation(value = "发送签名密钥")
    @PutMapping("sendKey/{id}")
    public Result lockHotelSet(@PathVariable Long id) {
        HotelSet hotelSet = hotelSetService.getById(id);
        String signKey = hotelSet.getSignKey();
        String zkcode = hotelSet.getZkcode();
        return Result.ok();
    }

}
